#!/bin/bash

APP=education

if [ -n "$2" ] ;then
   do_date=$2
else 
   echo "请传入日期参数"
   exit
fi 

dim_user_zip="
insert overwrite table ${APP}.dim_user_zip partition (dt='9999-12-31')
select 
    data.id,
    data.login_name,
    data.nick_name,
    md5(data.real_name) real_name,
    md5(data.phone_num) phone_num,
    md5(data.email) email,
    data.user_level,
    data.birthday,
    data.gender,
    data.status,
    data.create_time,
    data.operate_time,
    '$do_date' start_date,
    '9999-12-31' end_date
from ${APP}.ods_user_info_inc
where dt='$do_date'
and type='bootstrap-insert';
"

dim_province_full="
insert overwrite table ${APP}.dim_province_full partition(dt='$do_date')
select
        id, 
        name, 
        region_id, 
        area_code, 
        iso_code, 
        iso_3166_2
from
(
    select
        id, 
        name, 
        region_id, 
        area_code, 
        iso_code, 
        iso_3166_2
    from ${APP}.ods_base_province_full
    where dt='$do_date' 
);
"

dim_course_full="
insert overwrite table ${APP}.dim_course_full partition(dt='$do_date')
select
    id,
    course_name,
    course_slogan,
    subject_id,
    subject_name,
    category_id,
    category_name,
    teacher,
    publisher_id,
    chapter_num,
    origin_price,
    reduce_amount,
    actual_price,
    course_introduce,
    create_time,
    deleted,
    update_time
from
(
     select
        cour.id id,
        course_name,
        course_slogan,
        subject_id,
        sub.subject_name,
        cat.id category_id,
        cat.category_name category_name,
        teacher,
        publisher_id,
        chapter_num,
        origin_price,
        reduce_amount,
        actual_price,
        course_introduce,
        cour.create_time,
        cour.update_time,
        cour.deleted
    from ${APP}.ods_course_info_full cour 
    left join
        ${APP}.ods_base_subject_info_full sub 
    on cour.subject_id=sub.id
    left join 
        ${APP}.ods_base_category_info_full cat 
    on sub.category_id=cat.id
    where cour.dt='$do_date' 
    and sub.dt='$do_date' 
    and cat.dt='$do_date'
)t1;
"


case $1 in
"dim_user_zip")
    hive -e "$dim_user_zip"
;;
"dim_province_full")
    hive -e "$dim_province_full"
;;
"dim_course_full")
    hive -e "$dim_course_full"
;;
"all")
    hive -e "$dim_user_zip$dim_province_full$dim_course_full"
;;
esac